package org.bzjj.tool;

import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class TmriWrapHelper
{
  private static String Algorithm = "DES";
  private static String key = "*:@1$7!a";
  private static SecretKey skey = new SecretKeySpec(key.getBytes(), Algorithm);
  
  public static String Encrypt(String cleartext)
  {
    try
    {
      Cipher cipher = Cipher.getInstance(Algorithm);
      cipher.init(1, skey);
      byte[] cipherByte = cipher.doFinal(cleartext.getBytes());
      return URLEncoder.encode(new String(cipherByte, "ISO-8859-1"), "ISO-8859-1");
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
    return null;
  }
  
  public static String Decrypt(String ciphertext)
  {
    try
    {
      ciphertext = URLDecoder.decode(ciphertext, "ISO-8859-1");
      
      Cipher cipher = Cipher.getInstance(Algorithm);
      cipher.init(2, skey);
      byte[] clearByte = cipher.doFinal(ciphertext.getBytes("ISO-8859-1"));
      return new String(clearByte);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
    return null;
  }
  
  public static String encodeUTF8(String xmlDoc)
  {
    String str = "";
    try
    {
      return URLEncoder.encode(xmlDoc, "utf-8");
    }
    catch (Exception ex)
    {
      str = ex.toString();
    }
    return str;
  }
  
  public static String decodeUTF8(String str)
    throws Exception
  {
    String xmlDoc = "";
    try
    {
      xmlDoc = URLDecoder.decode(str, "utf-8");
    }
    catch (Exception ex)
    {
      xmlDoc = ex.toString();
    }
    return xmlDoc;
  }
  
  public static String byte2hex(byte[] b)
  {
    StringBuffer hs = new StringBuffer();
    String stmp = "";
    for (int n = 0; n < b.length; n++)
    {
      stmp = Integer.toHexString(b[n] & 0xFF);
      if (stmp.length() == 1)
      {
        hs.append("0");
        hs.append(stmp);
      }
      else
      {
        hs.append(stmp);
      }
    }
    return hs.toString().toUpperCase();
  }
  
  public static byte[] hex2byte(String strkey)
  {
    int keylength = strkey.length() / 2;
    String strValue = "";
    byte[] key = new byte[keylength];
    for (int i = 0; i < keylength; i++)
    {
      strValue = strkey.substring(2 * i, 2 * (i + 1));
      key[i] = Integer.valueOf(strValue, 16).byteValue();
    }
    return key;
  }
  
  public static String str2HexStr(String str)
  {
    char[] chars = "0123456789ABCDEF".toCharArray();
    StringBuilder sb = new StringBuilder("");
    byte[] bs = str.getBytes();
    for (int i = 0; i < bs.length; i++)
    {
      int bit = (bs[i] & 0xF0) >> 4;
      sb.append(chars[bit]);
      bit = bs[i] & 0xF;
      sb.append(chars[bit]);
    }
    return sb.toString();
  }
  
  public static String hexStr2Str(String hexStr)
  {
    String str = "0123456789ABCDEF";
    char[] hexs = hexStr.toCharArray();
    byte[] bytes = new byte[hexStr.length() / 2];
    for (int i = 0; i < bytes.length; i++)
    {
      int n = str.indexOf(hexs[(2 * i)]) * 16;
      n += str.indexOf(hexs[(2 * i + 1)]);
      bytes[i] = ((byte)(n & 0xFF));
    }
    return new String(bytes);
  }
}
